package com.sinog.auth.controller;

import com.sinog.auth.entity.AuthUser;
import com.sinog.auth.repository.UserRepository;
import com.sinog.core.util.AesUtil;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
import java.util.Map;

/**
 * @desc 认证授权中心获取登录用户信息的接口
 * @author zy
 * @date -
 */
@Slf4j
@RestController
public class UserController {

    private static final byte[] DECRYPTED = new byte[0];
    private static final byte[] DECRYPTED_1 = new byte[0];
    @Autowired
    private UserRepository repository;
    @Value("${gkzx.azbj.security.user-default-password-insert}")
    private String password;

    /**
     * 其他继承Oauth认证客户端获取用户的接口
     * @param principal principal
     * @return Object
     */
    @GetMapping("/user/current")
    public Object getUser(Principal principal) {
        return principal;
    }

    /**
     * 其他继承Oauth认证客户端修改用户密码
     * @param account account
     * @param oldpwd oldpwd
     * @param pwd pwd
     * @return String
     */
    @PostMapping("/user/password")
    public String changePwd(String account,String oldpwd,String pwd) {
        //前端传递密码解密
        String key = "abcdefghijklmn12";
        String iv = "abcdefghijklmn12";
        //解密
        byte[] decrypted = DECRYPTED;
        byte[] decrypted1 = DECRYPTED_1;
        try {
            decrypted = AesUtil.aesCbcDecrypt(AesUtil.decryptBase64(oldpwd),key.getBytes(),iv.getBytes());
            decrypted1 = AesUtil.aesCbcDecrypt(AesUtil.decryptBase64(pwd),key.getBytes(),iv.getBytes());
        } catch(Exception e) {
            log.error("发生异常:",e);
        }
        oldpwd = AesUtil.byteToString(decrypted);
        pwd = AesUtil.byteToString(decrypted1);
        BCryptPasswordEncoder bpe = new BCryptPasswordEncoder();
        String password = repository.findPasswordByAccount(account);
        boolean b = bpe.matches(oldpwd,password);
        if(b) {
            pwd = bpe.encode(pwd);
            int i = repository.updatePasswordByAccount(account,pwd);
            if(0 < i) {
                return JsonUtil.toText(ResultUtil.getResult(true));
            }
        }
        return JsonUtil.toText(ResultUtil.getResult(false));
    }

    /**
     * 其他继承Oauth认证客户端获取用户的接口
     * @param account account
     * @param pwd pwd
     * @return String
     */
    @PostMapping("/user/verifypwd")
    public String verifyPwd(String account,String pwd) {
        BCryptPasswordEncoder bpe = new BCryptPasswordEncoder();
        String password = repository.findPasswordByAccount(account);
        boolean b = bpe.matches(pwd,password);
        if(b) {
            return JsonUtil.toText(ResultUtil.getResult(true));
        }
        return JsonUtil.toText(ResultUtil.getResult(false));
    }

    /**
     * 其他继承Oauth认证客户端更新登录用户信息
     * @param data data
     * @return String
     */
    @PostMapping("/user/info")
    public String saveAuthUser(String data) {
        Map<String,Object> map = ResultUtil.getResult(false);
        AuthUser authUser = JsonUtil.toBean(data,AuthUser.class);
        if(null == authUser.getStat() || 1 == authUser.getStat()) {
            // 表示新增用户
            repository.insertAuthUser(authUser.getAccount(),password);
            map = ResultUtil.getResult(true);
        } else {
            //多次删除会报错,去除
            int count = repository.updateStatByAccount(authUser.getAccount(),authUser.getStat());
            if(0 < count) {
                map = ResultUtil.getResult(true);
            }
        }
        return JsonUtil.toText(map);
    }
}